package nbcp.handler

import nbcp.base.utils.VerifyCodeUtil
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.servlet.http.HttpServlet

/**
 * Created by udi on 17-4-6.
 */
@WebServlet(urlPatterns = arrayOf("/open/getValidateCodeImage"))
open class AuthImageServlet : HttpServlet() {

    override fun doGet(req: HttpServletRequest?, resp: HttpServletResponse?) {
        var request = req!!;
        var response = resp!!;

        response.setHeader("Pragma", "No-cache")
        response.setHeader("Cache-Control", "no-cache")
        response.setDateHeader("Expires", 0)
        response.contentType = "image/jpeg"

        //生成随机字串
        val verifyCode = VerifyCodeUtil.generateVerifyCode(4)
        //存入会话session
        val session = request.getSession(true)
        //删除以前的
        session.setAttribute("ValidateCode", verifyCode.toLowerCase());
        //生成图片
        val w = 100
        val h = 30
        VerifyCodeUtil.outputImage(w, h, response.outputStream, verifyCode)

    }
}

